<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>News</title>
    <link rel="stylesheet" type="text/css" href="../themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="../themes/icon.css">
    <script type="text/javascript" src="../jquery.min.js"></script>
    <script type="text/javascript" src="../jquery.easyui.min.js"></script>
    
	<script type="text/javascript" src="../jquery.serializejson.js"></script>
	<script type="text/javascript" src="../jquery.serializejson.min.js"></script>

	<script type="text/javascript" src="../../xheditor-1.2.2/xheditor-1.2.2.min.js"></script>
	<script type="text/javascript" src="../../xheditor-1.2.2/xheditor_lang/zh-cn.js"></script>
</head>

<body>
    <div class="easyui-layout" data-options="fit:true">
        <div data-options="region:'north'" style="height:50px">
            <a href="#" id="loadAllDataBtn" class="easyui-linkbutton" data-options="iconCls:'icon-add'">所有新闻</a>

            <a href="#" id="addBtn" class="easyui-linkbutton" data-options="iconCls:'icon-add'">添加新闻</a>
            <a href="#" id="removesBtn" class="easyui-linkbutton" data-options="iconCls:'icon-remove'">删除多个</a>

		<input class="easyui-searchbox" data-options="prompt:'请根据标题搜索',searcher:doSearch" style="width:300px">

        </div>
        <div data-options="region:'west',split:true" title="新闻分类" style="width:200px;">

            <ul id="tt"></ul>

        </div>
        <div data-options="region:'center',title:'Main Title',iconCls:'icon-ok'">
            <table id="dg"></table>

            <div id="dlg" class="easyui-dialog" title="Basic Dialog" data-options="iconCls:'icon-save',closed:true" style="width:500px;height:600px;padding:10px;">
                <form id="ff" method="post">
                    <div style="margin-bottom:20px">
                        <input class="easyui-textbox" name="_id" data-options="label:'id:'">
                        <!-- 添加处需要删除id -->
                        <br>
                        <br>
                        <br>
                        <input class="easyui-textbox" name="title" style="width:100%;" data-options="label:'标题:',required:true">
                    </div>
                    <div style="margin-bottom:20px">
                    	<textarea class="xheditor {skin:'default'} tx" name="desc" style="width:100%;height:60px" data-options="label:'新闻内容:',multiline:true"></textarea>
                        <!--<input class="easyui-textbox" name="desc" style="width:100%;height:60px" data-options="label:'新闻内容:',multiline:true">-->
                    </div>
                </form>
                <div style="text-align:center;padding:5px 0">
                    <a href="javascript:void(0)" class="easyui-linkbutton" onclick="submitForm()" style="width:80px">提交</a>
                    <a href="javascript:void(0)" class="easyui-linkbutton" onclick="clearForm()" style="width:80px">清除</a>
                </div>
            </div>
            
            <!--评论模块-->
            <div id="dlg1" class="easyui-dialog" title="Basic Dialog" data-options="iconCls:'icon-save',closed:true" style="width:1000px;height:600px;padding:10px;">
		        <div class="easyui-layout" data-options="fit:true">
			        <div data-options="region:'north'" style="height:50px">
			            <a href="#" id="loadAllDataBtn1" class="easyui-linkbutton" data-options="iconCls:'icon-add'">所有评论</a>
			
			            <a href="#" id="addBtn1" class="easyui-linkbutton" data-options="iconCls:'icon-add'">添加评论</a>
			            <a href="#" id="removesBtn1" class="easyui-linkbutton" data-options="iconCls:'icon-remove'">删除多个</a>
			
					<input class="easyui-searchbox" data-options="prompt:'请根据标题搜索',searcher:doSearch" style="width:300px">
			
			       </div>
			        <div data-options="region:'center',title:'Main Title',iconCls:'icon-ok'">
			            <table id="dg"></table>
			
			            <div id="dlg2" class="easyui-dialog" title="Basic Dialog" data-options="iconCls:'icon-save',closed:true" style="width:500px;height:600px;padding:10px;">
			                <form id="ff" method="post">
			                    <div style="margin-bottom:20px">
			                        <input class="easyui-textbox" name="_id" data-options="label:'id:'">
			                        <!-- 添加处需要删除id -->
			                        <br>
			                        <br>
			                        <br>
			                        <input class="easyui-textbox" name="title" style="width:100%;" data-options="label:'标题:',required:true">
			                    </div>
			                    <div style="margin-bottom:20px">
			                    	<textarea class="xheditor {skin:'default'} tx1" name="desc" style="width:100%;height:60px" data-options="label:'新闻内容:',multiline:true"></textarea>
			                        <!--<input class="easyui-textbox" name="desc" style="width:100%;height:60px" data-options="label:'新闻内容:',multiline:true">-->
			                    </div>
			                </form>
			                <div style="text-align:center;padding:5px 0">
			                    <a href="javascript:void(0)" class="easyui-linkbutton" onclick="submitForm()" style="width:80px">提交</a>
			                    <a href="javascript:void(0)" class="easyui-linkbutton" onclick="clearForm()" style="width:80px">清除</a>
			                </div>
			            </div>
			        </div>
			    </div>
            </div>
        </div>
    </div>
    <script>
    	//格式化日期
    	Date.prototype.format = function(fmt) { 
		     var o = { 
		        "M+" : this.getMonth()+1,                 //月份 
		        "d+" : this.getDate(),                    //日 
		        "h+" : this.getHours(),                   //小时 
		        "m+" : this.getMinutes(),                 //分 
		        "s+" : this.getSeconds(),                 //秒 
		        "q+" : Math.floor((this.getMonth()+3)/3), //季度 
		        "S"  : this.getMilliseconds()             //毫秒 
		    }; 
		    if(/(y+)/.test(fmt)) {
		            fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); 
		    }
		     for(var k in o) {
		        if(new RegExp("("+ k +")").test(fmt)){
		             fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
		         }
		     }
		    return fmt; 
		} 
    	
    	
        var cateId = null;
        var comId=null;
        $('#tt').tree({
            url: "http://localhost:3000/cate/list/1",
            method: 'get',
            onClick: function(node) {
                cateId = node._id;
                $("#dg").datagrid({
                    queryParams: {
                        cateId: cateId
                    }
                }).datagrid('reload');
            }
        });
        
        
        
        

        $('#dg').datagrid({
            url: 'http://localhost:3000/news/list',
            method: 'post',
            queryParams: {
                cateId: cateId
            },
            fit: true,
            pagination: true,
            pageSize: 10,
            pageList: [10, 20, 30],
            columns: [
                [{
                    field: 'ck',
                    title: '复选',
                    checkbox: true
                }, {
                    field: 'title',
                    title: '新闻标题',
                    width: 100
                }, {
                    field: 'desc',
                    title: '详情',
                    width: 500
                }, {
                    field: 'date',
                    title: '发布时间',
                    width: 500
                }, {
                    field: 'commitNumber',
                    title: '评论数',
                    width: 50
                }, {
                    field: 'operate',
                    title: '操作',
                    width: 100,
                    formatter: function(value, row, index) {
                        return "<a onclick=deleteData('" + row._id + "')>删除</a> <a onclick=editData('" + row._id + "')>修改</a> <a onclick=commitData('" + row._id + "')>评论</a>"
                    }
                }]
            ]
        });
        
        
        $('#dlg1 #dg').datagrid({
            url: 'http://localhost:3000/commit/list',
            method: 'post',
            queryParams: {
                comId: comId
            },
            fit: true,
            pagination: true,
            pageSize: 5,
            pageList: [5, 10, 15],
            columns: [
                [{
                    field: 'ck',
                    title: '复选',
                    checkbox: true
                }, {
                    field: 'newstitle',
                    title: '评论新闻内容',
                    width: 500
                }, {
                    field: 'desc',
                    title: '评论内容',
                    width: 500
                }, {
                    field: 'date',
                    title: '评论时间',
                    width: 500
                }, {
                    field: 'operate',
                    title: '操作',
                    width: 100,
                    formatter: function(value, row, index) {
                        return "<a onclick=deleteData('" + row._id + "')>删除</a> <a onclick=editData('" + row._id + "')>修改</a>"
                    }
                }]
            ]
        });
        
        

        $("#addBtn").click(function() {
            $('#ff').form('clear');
            if (cateId === null) {
                $.messager.show({
                    title: '信息提示',
                    msg: '请选择分类，否则无法添加新闻',
                    timeout: 3000,
                    showType: 'show'
                });
            } else {
                $('#dlg').dialog('open');
            }
        })



		


		
		function commitData(id){
//			console.log(id);
			
			
//			$.ajax({
//              type: "post",
//              url: "http://localhost:3000/commit/list",
//              async: true,
//              data: {comid:id}
//          }).done(function(res) {
//              $('#ff1').form('load', res);
//              $('#dlg1').dialog('open');
//              comId=id;
////              console.log(comId);
////              console.log(id);//当前新闻id
//          })
			$.ajax({
				type:"post",
				url:"http://localhost:3000/news/lists",
				async:true,
				data:{
					id:id
//					desc:desc
				}
			}).done(function(res){
//				console.log(res._id);
				console.log(res[0]._id);
				
				$.ajax({
					type:"post",
					url:"http://localhost:3000/commit/data",
					async:true,
					data:{
						comid:res[0]._id,
						newstitle:res[0].desc
					}
				}).done(function(res1) {
//					console.log(res1);
	              $('#dg1 #ff1').form('load', res1);
	              $('#dlg1').dialog('open');
//	              comId=id;
//	              console.log(comId);
//	              console.log(id);//当前新闻id
            	});
			});
			
//			$.ajax({
//              type: "post",
//              url: "http://localhost:3000/commit/data",
//              async: true,
//              data: {comid:id,newstitle:desc}
//          }).done(function(res) {
//              $('#dg1 #ff1').form('load', res);
//              $('#dlg1').dialog('open');
//              comId=id;
////              console.log(comId);
////              console.log(id);//当前新闻id
//          })''
			
            
//          $('#dlg1 #tt').tree({
//	            url: "http://localhost:3000/commit/list",
//	            method: 'post',
//	            onClick: function(node) {
////	                cateId = node._id;
//					comId=node._id;
//	                $("#dlg1 #dg").datagrid({
//	                    queryParams: {
//	                        comId: comId
//	                    }
//	                }).datagrid('reload');
//	            }
//	        });
		}


        function submitForm() {

            var postData = $("#ff").serializeJSON();
            postData.cateId = cateId;
//          console.log(postData);
            if (postData._id.length > 0) {
                // add
                delete postData.cateId;
                // edit
                postData.date=new Date().format("yyyy-MM-dd hh:mm:ss");
                $.ajax({
                    type: "put",
                    url: "http://localhost:3000/news/data/" + postData._id,
                    data: postData,
                    async: true
                }).done(function(res) {
//                  console.log(res);
                    
                    // 这里要做一件事
                    $.messager.show({
                        title: '信息提示',
                        msg: '数据修改成功',
                        timeout: 3000,
                        showType: 'show'
                    });
                    $('#dlg').dialog('close');
                    $('#dg').datagrid('reload');
                })

            } else {
                // add
                delete postData._id;
                if(postData.title!=""&&postData.desc!=""){
                	postData.date=new Date().format("yyyy-MM-dd hh:mm:ss");
//              	postData.commitNumber=0;
	                $.ajax({
	                    type: "post",
	                    url: "http://localhost:3000/news/data",
	                    data: postData,
	                    async: true
	                }).done(function(res) {
	                    // 这里要做一件事
                    	console.log(res);
	                    $.messager.show({
	                        title: '信息提示',
	                        msg: '数据添加成功',
	                        timeout: 3000,
	                        showType: 'show'
	                    });
	                    $('#dlg').dialog('close');
	                    $('#dg').datagrid('reload');
	                });
                }
                else{
                   		alert('请输入必要数据');
                   	}
            }

        }

        function clearForm() {
            $('#ff').form('clear');
            $('.tx').val('');
        }
        


        function deleteData(id) {

            $.messager.confirm('确认删除', '你确认删除数据吗？', function(r) {
                if (r) {
                    $.ajax({
                        type: "delete",
                        url: "http://localhost:3000/news/data/" + id,
                        async: true
                    }).done(function(res) {
                        $("#dg").datagrid('reload');
                    })
                }
            });
        }

        function editData(id) {
            $.ajax({
                type: "put",
                url: "http://localhost:3000/news/data/" + id,
                async: true
            }).done(function(res) {
                $('#ff').form('load', res);
                $('#dlg').dialog('open');
            })
        }

		

        $("#removesBtn").click(function() {
            var rows = $("#dg").datagrid('getSelections');

            if (rows.length > 0) {
                var ids = [];
                rows.forEach(function(item, idx) {
                    ids.push(item._id)
                })
//              console.log(ids);
                $.messager.confirm('确认删除', '你确认删除数据吗？', function(accept) {
                 	if(accept){
                 		$.ajax({
		                    type: "post",
		                    url: "http://localhost:3000/news/data/removes",
		                    data: {
		                        ids: ids.toString()
		                    },
		                    async: true
		                }).done(function(res) {
		                    $("#dg").datagrid('reload');
		                })
                 	}
                });
            }

        })

        $("#loadAllDataBtn").click(function() {
            cateId = null;
            $("#dg").datagrid({
                queryParams: {
                    cateId: cateId
                }
            }).datagrid('reload');
        })

        function doSearch(value) {
//      	console.log(value);
            $("#dg").datagrid({
            	url: 'http://localhost:3000/news/list',
            	method: 'post',
                queryParams: {
                    cateId: cateId,
                    title: value
                }
            }).datagrid('reload');
        }
    </script>
</body>

</html>